﻿#include "UChangeDevice.h"
#include "Global.h"
#include <qtimer.h>
UChangeDevice::UChangeDevice(QString sDllName, QString sDeviceId, bool bAdd,int nDeviceType, QWidget *parent)
	: m_sDllName(sDllName),m_sDeviceId(sDeviceId), m_bAdd(bAdd), m_nDeviceType(nDeviceType),QDialog(parent)
{
	ui.setupUi(this);
	m_pCurrDevice = nullptr;
	initUi();
}

UChangeDevice::~UChangeDevice()
{}

void UChangeDevice::initUi()
{
	/**
	* @ Author: Gaojia
	* @ Description: 初始化设备树
	*/
	m_pModel = new QStandardItemModel(this);
	QStandardItem *rootItem = m_pModel->invisibleRootItem();
	ui.treeView->setModel(m_pModel);
	ui.treeView->header()->setVisible(false);
	ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);  // ✅ 必须有
	buildItemTree(g_pRoot, rootItem);
	ui.treeView->expandAll();
	connect(ui.treeView, &QTreeView::clicked, this, &UChangeDevice::onTreeClickeTemple);
	connect(ui.pushButton, &QPushButton::clicked, this, &UChangeDevice::slot_clickedSave);
	connect(ui.pushButton_2, &QPushButton::clicked, this, &UChangeDevice::slot_clickedCancel);
}

void UChangeDevice::buildItemTree(CBaseNode * pNode, QStandardItem * pParentItem)
{
	if (!pNode) return;
	if (pNode->getType() == CBaseNode::enNode || pNode->getType() == CBaseNode::enRoot)
	{
		QStandardItem *pItem = new QStandardItem(pNode->getName());
		pItem->setData(QVariant::fromValue(reinterpret_cast<void*>(pNode)), Qt::UserRole);
		pParentItem->appendRow(pItem);
		for (CBaseNode *pChild : pNode->getChildren())
		{
			buildItemTree(pChild, pItem);
		}
	}
	else if (pNode->getType() == CBaseNode::enDevice)
	{
		CBaseDevice *pDevice = (CBaseDevice*)pNode;
		if ((pDevice->getDllName() == m_sDllName) && (pDevice->getDeviceType() == m_nDeviceType))
		{
			QStandardItem *pItem = new QStandardItem(pNode->getName());
			pItem->setData(QVariant::fromValue(reinterpret_cast<void*>(pNode)), Qt::UserRole);
			pParentItem->appendRow(pItem);
			if (pDevice->getId() == m_sDeviceId)
			{
				QModelIndex indexCurr = pItem->index(); // 获取该 item 对应的 QModelIndex
				// 延迟调用，保证布局完成
				QTimer::singleShot(0, [this, indexCurr]() {
					ui.treeView->scrollTo(indexCurr, QAbstractItemView::PositionAtCenter);
					ui.treeView->setCurrentIndex(indexCurr);
					});
				ui.treeView->setCurrentIndex(indexCurr);   // 可选：高亮选中
				ui.lineEdit->setText(pNode->getName());
				m_pCurrDevice = pDevice;
			}
			if (m_pCurrDevice == NULL)
			{
				QModelIndex indexCurr = pItem->index(); // 获取该 item 对应的 QModelIndex
				// 延迟调用，保证布局完成
				QTimer::singleShot(0, [this, indexCurr]() {
					ui.treeView->scrollTo(indexCurr, QAbstractItemView::PositionAtCenter);
					ui.treeView->setCurrentIndex(indexCurr);
					});
				ui.treeView->setCurrentIndex(indexCurr);   // 可选：高亮选中
				ui.lineEdit->setText(pNode->getName());
				m_pCurrDevice = pDevice;
			}
		}
	}
	
}

void UChangeDevice::slot_clickedSave()
{
	if (m_pCurrDevice != NULL)
	{
		if (m_bAdd)
		{
			emit sign_addDevice(m_pCurrDevice->getId());
		}
		else
		{
			emit sign_selectDevice(m_pCurrDevice->getId());
		}
		close();
	}
	else
	{
		showMessageBox(this, A2T("警告"), A2T("请正确选择设备！"), 1);
		return;
	}
	
}

void UChangeDevice::slot_clickedCancel()
{
	close();
}

void UChangeDevice::onTreeClickeTemple(const QModelIndex & index)
{
	if (!index.isValid()) return;
	QStandardItem* item = m_pModel->itemFromIndex(index);
	if (!item) return;
	CBaseNode* node = reinterpret_cast<CBaseNode*>(item->data(Qt::UserRole).value<void*>());
	if (!node) return;
	if (node->getType() == CBaseNode::enDevice)
	{
		m_pCurrDevice = (CBaseDevice*)node;
		ui.lineEdit->setText(m_pCurrDevice->getName());
	}
	else
	{
		m_pCurrDevice = nullptr;
		ui.lineEdit->setText("");
	}
}
